package com.bbx.taxi.client.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import butterknife.InjectView;

import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.official.R;
import com.example.Util.DensityUtil;
import com.example.mylistview.pullListView.PullToRefreshLayout;
import com.example.mylistview.swipemenu.MyListView;
import com.example.mylistview.swipemenu.SwipeMenu;
import com.example.mylistview.swipemenu.SwipeMenuCreator;
import com.example.mylistview.swipemenu.SwipeMenuItem;
import com.example.mylistview.swipemenu.MyListView.OnMenuItemClickListener;
import com.example.mylistview.swipemenu.MyListView.OnSwipeListener;
import com.bbx.taxi.client.Adapter.CommonContactAdapter;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.ContactPersonMsg;
import com.bbx.taxi.client.DB.ContactsDB;
import com.bbx.taxi.client.DB.Util.DBcolumns;

/** 
 * @Path history_contacts.HistoryContactsActivity.java
 * @Author 严旭文
 * @Date 2016-2-2 下午4:37:55
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class HistoryContactsActivity extends BaseBBXActivity implements OnItemClickListener{
	@InjectView(R.id.layout_nodata)
	LinearLayout layout_nodata;
	private CommonContactAdapter mCommonContactAdapter;
	private ContactsDB db_contacts;
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.history_contacts);
		super.onCreate(savedInstanceState);
	}
	@Override
	protected void initView() {
		setTitle(true, "联系人", -1, "清空");
		mCommonContactAdapter = new CommonContactAdapter(this, list);
		mMyListView.setAdapter(mCommonContactAdapter);
		mMyListView.setOnItemClickListener(this);
		mMyListView.hideLoad();
		mMyListView.hideRefresh();
		mMyListView.setEmptyView(layout_nodata);
		initswipe();
		
	}

	@Override
	protected void initData() {
		db_contacts = new ContactsDB(this);
		readAllDB();
		
	}
	/**
	 * 初始化ListView的Item菜单
	 */
	public void initswipe() {
        super.initswipe();
		// step 2. listener item click event
		mMyListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(final int position, SwipeMenu menu,
					int index) {
				// ApplicationInfo item = mAppList.get(position);
				switch (index) {
				case 0:
					onDelete(list.get(position).get(DBcolumns.CONTACTS_ID),
							position);
					Toast.makeText(context, "删除成功",
							Toast.LENGTH_SHORT).show();
					mHandler.postDelayed(new Runnable() {
						@Override
						public void run() {
							mMyListView.closeMenu();
						}
					}, 200);
					break;

				}
			}
		});
	
	}
    @Override
    public void onClick(View v) {
    	switch(v.getId()){
    	case R.id.tv_right:
    		onDelete(Value.CONTACTS_TYPE_HISTORY);
    		break;
    		default :
    			super.onClick(v);
    			break;
    	}
    	
    }
	/**
	 * 读取所有数据
	 */
	public void readAllDB() {
		list.clear();
		Cursor c = db_contacts.onQuery(Value.CONTACTS_TYPE_COLLECTE);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				list.add(map);
			} while (c.moveToNext());
		}
		c = db_contacts.onQuery(Value.CONTACTS_TYPE_HISTORY);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				list.add(map);
			} while (c.moveToNext());
		}
		mCommonContactAdapter.notifyDataSetChanged();
		c.close();
		db_contacts.onColse();
	}

	/**
	 * 写入数据库
	 */
	public void onWriteDB(String name, String tel, String collection) {
		db_contacts.onWrite(name, tel, collection);
	}

	/**
	 * 更新数据(顺序)
	 */
	public void onUpdate_sequence(String id, String name, String tel,
			String collection) {
		db_contacts.onUpdate_sequence(id, name, tel, collection);
	}



	/**
	 * 根据收藏或历史进行删除
	 */
	public void onDelete(String isCollection) {
		db_contacts.onDeleteType(isCollection);
		readAllDB();
	}

	/**
	 * 删除一个数据
	 */
	public void onDelete(String rowId, int position) {
		db_contacts.onDelete(rowId);
		list.remove(position);
		mCommonContactAdapter.notifyDataSetChanged();
//		if (getCount() == 0) {
//			mMyListView.removeFooterView(clearView);
//			layout_prompt.setVisibility(View.GONE);
//		} else {
//			mMyListView.addFooterView(clearView, null, false);
//			layout_prompt.setVisibility(View.VISIBLE);
//		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		String contacts_id = mCommonContactAdapter.getItem(position).get(
				DBcolumns.CONTACTS_ID);
		String contacts_name = mCommonContactAdapter.getItem(position).get(
				DBcolumns.CONTACTS_NAME);
		String contacts_tel = mCommonContactAdapter.getItem(position).get(
				DBcolumns.CONTACTS_TEL);
		String contacts_collection = mCommonContactAdapter.getItem(position)
				.get(DBcolumns.CONTACTS_COLLECTION);
		onUpdate_sequence(contacts_id, contacts_name, contacts_tel,
				contacts_collection);
		Bundle bundle = new Bundle();
		bundle.putString(ContactPersonMsg.extra_contact_name, contacts_name);
		bundle.putString(ContactPersonMsg.extra_contact_phone, contacts_tel);
		Intent intent = new Intent();
		intent.putExtras(bundle);
		setResult(resultCode, intent);
		onfinish();
	}
}

